package so.sao.code.service.infrastructure.stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import so.sao.code.service.infrastructure.stream.channel.ThirdPartyTagOutChannel;
import so.sao.top.core.util.mapper.JsonMapper;

/**
 * Created by Administrator on 2017/12/8.
 */
@EnableBinding(value = ThirdPartyTagOutChannel.class)
public class ProduceManager {

    private final static Logger LOGGER = LoggerFactory.getLogger(ProduceManager.class);
    @Autowired
    ThirdPartyTagOutChannel thirdPartyTagOutChannel;

    @Autowired
    JsonMapper jsonMapper;

    public boolean send(Object msg) {
        try {
            String value = jsonMapper.toJson(msg);
            return thirdPartyTagOutChannel.output().send(MessageBuilder.withPayload(value).build());
        } catch (Exception ex) {
            LOGGER.info("cocacola message error {}", ex.getMessage());
            throw ex;
        }
    }

    
    public boolean send(Object msg,Long timeout) {
        try {
            String value = jsonMapper.toJson(msg);
            return thirdPartyTagOutChannel.output().send(MessageBuilder.withPayload(value).build(),timeout);
        } catch (Exception ex) {
            LOGGER.info("cocacola message error {}", ex.getMessage());
            throw ex;
        }
    }

}
